package com.sk.ccflower.server.lib.sysUtils;

import com.sk.ccflower.lib.commonUtils.StringUtil;
import com.sk.ccflower.server.lib.sysBeans.CpuLoadBean;
import com.sk.ccflower.server.lib.sysBeans.CpuStatBean;
import com.sk.ccflower.server.lib.sysBeans.ProcCpuLoadBean;
import com.sk.ccflower.server.lib.sysBeans.ProcStatBean;

/**
 * @author eunwoogi
 * @since 2009. 11. 23.
 */
public class SysResCollectImpl_Windows implements SysResCollectFacade {
	
	public int getPid( String name ) throws Exception {
		WinCollectDllLoader loader = new WinCollectDllLoader();
		return loader.getProcessId(name);
	}
	
	public String getCpuStat() throws Exception {
		return null;
	}
	
	public CpuStatBean parseCpuStat( String stat ) throws Exception {
		CpuStatBean data = new CpuStatBean();
		
		return data;
	}
	
	public CpuLoadBean getCpuLoad( CpuStatBean oldData, CpuStatBean newData ) throws Exception {
		CpuLoadBean load = new CpuLoadBean();
		
		return load;
	}
	
	public String getProcStat( int pid ) throws Exception {
		if ( pid == -1 )	return null;
		
		WinCollectDllLoader loader = new WinCollectDllLoader();
		
		String stat = "";
		stat += loader.getProcessCpuTime(pid) + " ";
		stat += loader.getProcessCpuCounter(pid) + " ";
		stat += loader.getProcessMem(pid) + "";
		
		return stat;
	}
	
	public ProcStatBean parseProcStat( String stat ) throws Exception {
		ProcStatBean data = new ProcStatBean();
		
		if ( null == stat )	return data;
		
		String[] values = StringUtil.toStringArray( stat, " " );
		
		data.ucpu = new Long( values[0] ).longValue();
		data.scpu = new Long( values[1] ).longValue();
		data.rmem = new Long( values[2] ).longValue();
		
		return data;
	}
	
	public ProcCpuLoadBean getProcCpuLoad( ProcStatBean oldProc, ProcStatBean newProc, CpuStatBean oldCpu, CpuStatBean newCpu ) throws Exception {
		ProcCpuLoadBean procLoad = new ProcCpuLoadBean();
		
		long counter = newProc.scpu - oldProc.scpu;
		long time = newProc.ucpu - oldProc.ucpu;
		float tmp_val = (float)counter / (float)time;
		
		procLoad.system = (float) ((1.0 - tmp_val) * 100);
		procLoad.user = 0;
		
		return procLoad;
	}
	
}
